<template>
  <el-container>
    <el-header>
      <el-col :span="4" class="logo">
        <h1>Homes</h1>
      </el-col>
      <el-col :span="20">
        <el-col :span="4" :offset="20" class="userInfo">
          <el-dropdown>
            <span class="el-dropdown-link">
              {{userInfo.username}}
              <i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <router-link to="/userInfo">
                  我的资料
                </router-link>
              </el-dropdown-item>
              <el-dropdown-item>
                <router-link to="/resetPassword">
                  修改密码
                </router-link>
              </el-dropdown-item>
              <el-dropdown-item divided><span @click="logout">退出登录</span></el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-col>
    </el-header>
    <el-container class="container">

      <el-aside :span="4" width="200px">
        <el-menu>
          <el-submenu class="menu_item" index="1">
            <template slot="title">
              <i class="el-icon-document"></i>
              用户管理
            </template>
            <el-menu-item index="1-1">
              <router-link to="/userInfo">
                我的资料
              </router-link>
            </el-menu-item>
            <el-menu-item index="1-2">
              <router-link to="/resetPassword">
                修改密码
              </router-link>
            </el-menu-item>
          </el-submenu>
          <el-menu-item class="menu_item" index="2">
            <router-link to="apartmentList">
              <i class="el-icon-menu"></i>
              <span>我的别墅</span>
            </router-link>
          </el-menu-item>
          <el-menu-item class="menu_item" index="3">
            <router-link to="orderList">
              <i class="el-icon-date"></i>
              <span>我的订单</span>
            </router-link>
          </el-menu-item>
        </el-menu>
      </el-aside>
      <el-main :span="20">
        <router-view :userInfo = "userInfo" v-if = "isGetData"></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
  import ElSubmenu from '../../../node_modules/element-ui/packages/menu/src/submenu.vue';

  export default {
    components: {ElSubmenu},
    data() {
      return {
        userInfo: {},
        isGetData: false
      };
    },
    created() {
      // 获取用户信息
      this.getUserInfo();
    },
    methods: {
      getUserInfo() {
        this.$http.get('/api/host/user/getUserInfo.do')
          .then((response) => {
            var data = response.data;
            this.userInfo = {...data.data};
            this.isGetData = true;
            console.log(this.userInfo);
          })
          .catch((error) => {
            console.log(error.data);
          });
      },
      logout() {
        this.$http.get('/api/host/user/logout.do')
          .then((response) => {
            var data = response.data;
            if (data.status === 0) {
              this.$message({
                message: '退出成功',
                tpye: 'success',
                duration: 1000
              });
              setTimeout(() => {
                this.$router.push({path: '/login'});
              }, 1000);
            }
          })
          .catch((error) => {
            console.log(error.data);
          });
      }
    }
  };
</script>

<style>
  #app {
    overflow: hidden;
    margin: 0;
    padding: 0;
  }
  a {
    display: block;
  }
  a:hover{
    color: #66b1ff;
  }
  .menu_item{
    text-align: left;
  }
  .el-header, .el-footer {
    background-color: #B3C0D1;
    color: #333;
    text-align: center;
    line-height: 60px;
  }
  .container{
    position: absolute;
    top: 60px;
    left: 0;
    bottom: 0;
    right: 0px;
  }
  .el-aside {
    background-color: #D3DCE6;
    color: #333;
    text-align: center;
    line-height: 200px;
  }

  .el-main {
    background-color: #E9EEF3;
    color: #333;
    text-align: center;
    line-height: 160px;
  }

  body > .el-container {
    margin-bottom: 40px;
  }

</style>
